﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Assignment1.CurrencyConvertorService;

namespace Assignment1
{
    public partial class CurrencyConvertorForm : Form
    {
        public CurrencyConvertorForm()
        {
            InitializeComponent();
        }

        //************************************************************************
        //*
        //* Convert button was clicked.  Query the web service for the conversion
        //* rates requested on the amount entered
        //*
        //************************************************************************

        private void _convertButton_Click(object sender, EventArgs e)
        {
            try
            {
                double amount = double.Parse(_currencyAmount.Text);

                // create the soap client for the convertor
                CurrencyConvertorSoapClient client =
                    new CurrencyConvertorSoapClient("CurrencyConvertorSoap");

                // get the conversion rate
                double rate =
                    client.ConversionRate(
                        _stringToCurrency(_from.Text),
                        _stringToCurrency(_to.Text));

                double result = amount * rate;

                _results.Text = result.ToString();

            }
            catch (Exception ex)
            {
                String message =
                    "Exception occured while converting currency.  Exception was: " +
                    ex.Message;

                Console.WriteLine(message);
            }
        }

        //************************************************************************
        //*
        //* Get the Currency representation of the suppied string <type>.
        //* Return USD if the currency isn't found.
        //*
        //************************************************************************

        private Currency _stringToCurrency(String type)
        {
            Currency currency = Currency.USD;

            type = type.ToLower();

            if (type == "usd") currency = Currency.USD;
            else if (type == "aud") currency = Currency.AUD;
            else if (type == "gbp") currency = Currency.GBP;
            else if (type == "cad") currency = Currency.CAD;
            else if (type == "eur") currency = Currency.EUR;
            else if (type == "jpy") currency = Currency.JPY;
            else if (type == "cny") currency = Currency.CNY;

            return currency;
        }

    }
}
